import http from './http';
import { requestWithRetry } from '@/utils/retry';

// 组织管理API
export const organizationAPI = {
  // 获取组织列表
  getOrganizations: async (params: any) => {
    try {
      // 使用重试机制获取组织列表
      const response = await requestWithRetry(
        () => http.get('/organizations', { params }),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取组织列表失败'
      };
    }
  },

  // 获取组织详情
  getOrganization: async (id: string) => {
    try {
      // 使用重试机制获取组织详情
      const response = await requestWithRetry(
        () => http.get(`/organizations/${id}`),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取组织详情失败'
      };
    }
  },

  // 创建组织
  createOrganization: async (data: any) => {
    try {
      const result = await http.post('/organizations', data);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '创建组织失败'
      };
    }
  },

  // 更新组织
  updateOrganization: async (id: string, data: any) => {
    try {
      const result = await http.put(`/organizations/${id}`, data);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '更新组织失败'
      };
    }
  },

  // 删除组织
  deleteOrganization: async (id: string) => {
    try {
      const result = await http.delete(`/organizations/${id}`);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '删除组织失败'
      };
    }
  },

  // 获取用户所属的组织
  getUserOrganizations: async (userId?: string) => {
    try {
      // 如果没有提供userId或userId为'current'，则获取当前用户所属的组织
      const url = userId && userId !== 'current' 
        ? `/users/${userId}/organizations` 
        : '/users/current/organizations';
      
      // 使用重试机制获取用户所属的组织
      const response = await requestWithRetry(
        () => http.get(url),
        3, // 最多重试3次
        1000 // 基础延迟1秒
      );
      
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取用户所属组织失败'
      };
    }
  },

  // 添加用户到组织
  addUserToOrganization: async (data: any) => {
    try {
      const result = await http.post('/organizations/add-user', data);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '添加用户到组织失败'
      };
    }
  },

  // 从组织移除用户
  removeUserFromOrganization: async (data: any) => {
    try {
      const result = await http.post('/organizations/remove-user', data);
      return {
        success: true,
        data: result
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '从组织移除用户失败'
      };
    }
  },

  // 获取用户在组织中的角色
  getUserRoleInOrganization: async (userId: number, organizationId: number) => {
    try {
      const response = await http.get(`/users/${userId}/organizations/${organizationId}/role`);
      return {
        success: true,
        data: response.data
      };
    } catch (error: any) {
      return {
        success: false,
        data: null,
        message: error.message || '获取用户角色失败'
      };
    }
  }
}

export default organizationAPI